<template>
  <div class="login">
    <div class="left">
      <div class="title">
        <img class="t1" src="@/assets/images/login-logo.png" alt />
        <span class="t2">黑马面面</span>
        <div class="t3"></div>
        <span class="t4">用户登录</span>
      </div>
      <el-form class="login-form" :rules="rules" :model="form" ref="form">
        <el-form-item prop="phone">
          <el-input
            class="phone"
            placeholder="请输入手机号"
            prefix-icon="el-icon-user"
            v-model="form.phone"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            class="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
            v-model="form.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="17">
              <el-input
                class="code"
                placeholder="请输入验证码"
                prefix-icon="el-icon-key"
                v-model="form.code"
              ></el-input>
            </el-col>
            <el-col :span="7">
              <img @click="getTxcode" class="login-code" :src="baseURL+codeURL" alt />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass">
            <span>我已阅读并同意</span>
          </el-checkbox>
          <el-link class="link" :underline="false" type="primary">用户协议</el-link>和
          <el-link class="link" :underline="false" type="primary">隐私条款</el-link>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn btn-login" @click="toLogin">登录</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn btn-register" @click="toRegister">注册</el-button>
        </el-form-item>
      </el-form>
      <register ref="register"></register>
    </div>
    <img class="right" src="@/assets/images/login01.png" />
  </div>
</template>
<script>
import register from "@/views/login/register";
import { tologin } from "@/api/login";
import { setLocal, getLocal } from "@/utils/local";
export default {
  components: {
    register,
  },
  data() {
    return {
      baseURL: process.env.VUE_APP_URL,
      codeURL: "/captcha?type=login",
      form: {
        phone: "",
        password: "",
        code: "",
        isPass: [],
      },
      rules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (reg.test(value)) {
                callback();
              } else {
                callback(new Error("手机号格式错误"));
              }
            },
            trigger: "change",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          { min: 6, max: 12, message: "请输入6-12位密码", trigger: "change" },
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "change" },
          { min: 4, max: 4, message: "请输入4位验证码", trigger: "change" },
        ],
        isPass: [
          {
            required: true,
            message: "请先同意用户协议",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    getTxcode() {
      this.codeURL = "/captcha?type=login&aaa=" + Math.random() * 999;
    },
    toLogin() {
      this.$refs.form.validate((res) => {
        if (res) {
          this.$message.success("验证成功");
          let data = this.form;
          tologin(data)
            .then((res) => {
              // console.log(res);
              setLocal("token", res.data.data.token);
              this.$router.push("/layout");
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          this.$message.error("信息有误,请重新填写");
          return false;
        }
      });
    },
    toRegister() {
      this.$refs.register.isShow = true;
    },
  },
  created() {
    // console.log(getLocal('token'));
    if (getLocal("token")) {
      this.$router.push("/layout");
    }
  },
};
</script>
<style lang="less">
.login {
  height: 100%;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  display: flex;
  justify-content: space-around;
  align-items: center;
  .left {
    width: 480px;
    height: 550px;
    background-color: #f5f5f5;
    padding: 42px;
    .title {
      display: flex;
      align-items: center;
      margin-bottom: 30px;
      .t1 {
        margin-top: 5px;
        width: 25px;
        height: 18px;
        margin-right: 13px;
      }
      .t2 {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
      .t3 {
        width: 1px;
        height: 28px;
        background: #c7c7c7;
        margin: 0 15px;
      }
      .t4 {
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
    }
    .login-form {
      .login-code {
        width: 100%;
        height: 40px;
      }
      .link {
        padding: 0 2px 2px;
      }
      .btn {
        width: 100%;
      }
    }
  }
}
</style>